#include "editdept.h"
#include "ui_editdept.h"

#include <QMouseEvent>

EditDept::EditDept(QList<DictData> sys_normal_disable,int deptId,QString parentName,
                   int parentId,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::EditDept)
{
    ui->setupUi(this);
    setWindowIcon(QIcon(":/image/res/stm.jpg"));
    setWindowTitle("编辑部门");
    this->sys_normal_disable = sys_normal_disable;
    this->deptId = deptId;
    if(deptId > 0) ui->label->setText("修改部门");
    if(deptId == 100){
        ui->label_2->hide();
        ui->widget_14->hide();
        setFixedSize(712,288);
    }
    tree = new QTreeWidget(this);
    if(!parentName.isEmpty() && parentId > 0){
        ui->lineEdit_parentDept->setText(parentName);
        dept.parentName = parentName;
        dept.parentId = parentId;
    }
    init();

    ui->lineEdit_parentDept->installEventFilter(this);

    connect(tree,&QTreeWidget::itemClicked,this,[=](QTreeWidgetItem *item){
        dept.parentId = item->data(1,0).toInt();
        ui->lineEdit_parentDept->setText(item->text(0));
        tree->hide();
    });
}

EditDept::~EditDept()
{
    delete ui;
}

bool EditDept::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonPress){
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        if(mouseEvent->button() == Qt::LeftButton){
            if(obj == ui->lineEdit_parentDept){
                if(tree->isHidden()){
                    ui->lineEdit_parentDept->clear();
                    tree->setFixedSize(ui->widget_14->width(),250);
                    tree->show();
                    tree->move(ui->widget_14->geometry().bottomLeft());
                }else{
                    tree->hide();
                }
            }
            if(obj == ui->label_deptMenu){
                ui->lineEdit_parentDept->clear();
            }
            return true;
        }else{
            return false;
        }
    }else if(event->type() == QEvent::Enter){
        return true;
    }else if(event->type() == QEvent::Leave){
        if(obj == tree){
            tree->close();
        }
        return true;
    }else{
        return QObject::eventFilter(obj, event);
    }
}

void EditDept::init()
{
    ui->radioButton_status1->setText(sys_normal_disable.at(0).dictLabel);
    ui->radioButton_status0->setText(sys_normal_disable.at(1).dictLabel);
    getList();


    if(deptId > 0){ //修改
        QByteArray *data = api.getById(deptId);
        QJsonObject obj = QJsonDocument::fromJson(*data).object();
        if(obj.value("code").toInt() != 200){
            return;
        }
        dept = Dept::toDept(obj.value("data").toObject());
        ui->lineEdit_parentDept->setText(dept.parentName);
        ui->lineEdit_deptName->setText(dept.deptName);
        ui->spinBox_deptSort->setValue(dept.orderNum);
        ui->lineEdit_header->setText(dept.leader);
        ui->lineEdit_phone->setText(dept.phone);
        ui->lineEdit_email->setText(dept.email);
        dept.status == "0" ? ui->radioButton_status1->setChecked(1) : ui->radioButton_status0->setChecked(1);
    }
    initTree();
}

void EditDept::getList()
{
    deptList.clear();
    QByteArray *data = api.list("","");
    QJsonObject obj = QJsonDocument::fromJson(*data).object();
    if(obj.value("code").toInt() != 200){
        return;
    }
    QJsonArray arr = obj.value("data").toArray();
    for(int i = 0;i< arr.size();i++){
        deptList.append(Dept::toDept(arr.at(i).toObject()));
    }
}

void EditDept::initTree()
{
    tree->hide();
    tree->setHeaderHidden(1);
    tree->clear();
    for(int i = 0;i< deptList.size();i++){
        if(deptList.at(i).parentId == 0){
            QTreeWidgetItem *item = new QTreeWidgetItem(tree);
            item->setText(0,deptList.at(i).deptName);
            item->setData(1,0,deptList.at(i).deptId);
            setChildTree(item);
        }
    }
}

void EditDept::setChildTree(QTreeWidgetItem *item)
{
    for(int i = 0;i< deptList.size();i++){
        if(deptList[i].deptName == dept.deptName){
            continue;
        }
        if(deptList.at(i).parentId == item->data(1,0).value<int>()){
            QTreeWidgetItem *child = new QTreeWidgetItem(item);
            child->setText(0,deptList.at(i).deptName);
            child->setData(1,0,deptList.at(i).deptId);
            setChildTree(child);
        }
    }
}

void EditDept::on_btn_cancel_clicked()
{
    close();
}


void EditDept::on_btn_ok_clicked()
{
    if(ui->lineEdit_parentDept->text().isEmpty()){
        ui->widget_14->setStyleSheet("background-color: rgb(255, 255, 255);border: 1px solid red;border-radius:15px;");
        return;
    }else{
        ui->widget_14->setStyleSheet("background-color: rgb(255, 255, 255);border: 1px solid gray;border-radius:15px;");
    }
    dept.deptName = ui->lineEdit_deptName->text();
    dept.orderNum = ui->spinBox_deptSort->value();
    dept.leader = ui->lineEdit_header->text();
    dept.phone = ui->lineEdit_phone->text();
    dept.email = ui->lineEdit_email->text();
    dept.status = ui->radioButton_status1->isChecked() ? "0" : "1";
    QByteArray *data = api.editDept(deptId,dept);
    QJsonObject obj = QJsonDocument::fromJson(*data).object();
    if(obj.value("code").toInt() == 200){
        QMessageBox::information(0,"提示",obj.value("msg").toString());
        emit success();
        close();
    }
}

